package com.yishusmt.mobsmt.serviceimpl;

import com.yishusmt.mobsmt.check.DataCheck;
import com.yishusmt.mobsmt.dto.base.Result;
import com.yishusmt.mobsmt.dto.request.EditPasswordReq;
import com.yishusmt.mobsmt.dto.request.UserInfoReq;
import com.yishusmt.mobsmt.dto.request.UserLoginReq;
import com.yishusmt.mobsmt.dto.response.UserRes;
import com.yishusmt.mobsmt.entity.UserEntity;
import com.yishusmt.mobsmt.enums.ResultCodeEnum;
import com.yishusmt.mobsmt.mapper.UserEntityMapper;
import com.yishusmt.mobsmt.mapper.UserInfoMapper;
import com.yishusmt.mobsmt.service.UserManageService;
import com.yishusmt.mobsmt.utils.MD5Util;
import com.yishusmt.mobsmt.utils.TipsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户信息管理
 *
 * @author zhangyi
 * @date 2018/8/14 17:53
 */
@Service
public class UserManageServiceImpl implements UserManageService {


    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserEntityMapper userEntityMapper;

    /**
     * 获取用户信息
     *
     * @param phone
     * @return
     */
    @Override
    public Result getUserInfo(String phone) {
        return new Result(userInfoMapper.selectUserSelective(phone));
    }

    /**
     * 用户信息编辑
     *
     * @param userRes
     * @return
     */
    @Override
    public Result editUserInfo(UserInfoReq userRes) {
        UserEntity userEntity = new UserEntity();

        if(userRes.getPhone() != null && userRes.getPhone() != ""){
            UserRes user = userInfoMapper.selectUserSelective(userRes.getPhone());
            userEntity.setCompanyName(userRes.getCompanyName());
            userEntity.setCompanyAddress(userRes.getCompanyAddress());
            userEntity.setContactName(userRes.getContactName());
            userEntity.setContactPhone(userRes.getContactPhone());
            userEntity.setQq(userRes.getQq());
            userEntity.setWechat(userRes.getWechat());
            userEntity.setEmail(userRes.getEmail());
            if(user != null){
                userEntity.setId(user.getId());
            }
        }
        int i = userEntityMapper.updateByPrimaryKeySelective(userEntity);
        return 0==i?TipsUtil.returnTips(ResultCodeEnum.OPERATION_FAIL,"编辑失败！")
                : TipsUtil.returnTips(ResultCodeEnum.OPERATION_SUCCESS,"编辑成功！");
    }

    /**
     * 密码修改
     *
     * @param editPasswordReq
     * @return
     */
    @Override
    public Result editPassword(EditPasswordReq editPasswordReq) {
        DataCheck.isNull(editPasswordReq);
        //登录用户，比较手机号和密码
        UserLoginReq userLoginReq = new UserLoginReq(editPasswordReq.getPhone(),editPasswordReq.getOldPassword());
        UserEntity userEntity = userInfoMapper.selectUser(userLoginReq);
        if(userEntity != null) {
            //临时匹配用户信息
            UserEntity userEntity1 = new UserEntity();
           userEntity1.setId(userEntity.getId());
            String newPassword = editPasswordReq.getNewPassword();
            String s = MD5Util.encryptString(newPassword);
            userEntity1.setPassword(s);
            int i = userEntityMapper.updateByPrimaryKeySelective(userEntity1);
            return 0==i?TipsUtil.returnTips(ResultCodeEnum.OPERATION_FAIL,"修改失败！"):
                    TipsUtil.returnTips(ResultCodeEnum.OPERATION_SUCCESS,"修改成功!");
        }
        return TipsUtil.returnTips(ResultCodeEnum.OPERATION_FAIL,"用户不存在!");
    }

    /**
     * 绑定信息
     *
     * @param phone
     * @param contact
     * @return
     */
    @Override
    public Result bing(String phone, String contact) {
        //待定。。。
        return null;
    }
}
